<template>
  <view class="message">
    <!-- 头部公告消息 -->
    <notice></notice>

    <uniIcons type="contact" size="30"></uniIcons>

    <!-- 接受到的消息列表 -->
    <messages
      v-for="(item, index) in messageList"
      :key="index"
      :icon="item.icon"
      :content="'adad'"
      :title="item.message"
      :index="index"
      :typed="item.typed"
      :time="item.time"
      :sum="item.sum"
      @on-click="toChat"
    ></messages>

    {{}}
  </view>
</template>

<!-- 消息页面 -->
<script lang="ts" setup>
import messages from '@/components/messages/messages.vue'
import notice from '@/components/notice/notice.vue'
import type { Messages } from '@/config/data/data'
import {
  onLoad,
  onReady,
  onShow,
  onHide,
  onUnload,
  onPageScroll,
} from '@dcloudio/uni-app'
import { ref } from 'vue'

//跳转聊天界面
const toChat = (index: number) => {
  uni.navigateTo({
    url: `/subpkg/chat/chat?title=${messageList.value[index].message}&id=${messageList.value[index].id}`,
  })
}

//接受到的消息列表类型
const messageList = ref<Messages[]>([
  {
    icon: 'https://www.lyzhnb.top/mmexport1668477585298.jpg',
    id: 2,
    typed: 'admin',
    time: new Date(),
    sum: 1,
    message: '软星站长',
  },
  {
    icon: '../../static/images/message/notic.png',
    id: 4,
    typed: 'notice',
    time: new Date(),
    sum: 1,
    message: '本站通告',
  },
  {
    icon: '../../static/images/message/goods.png',
    id: 3,
    typed: 'goods',
    time: new Date(),
    sum: 1,
    message: '产品咨询',
  },
  {
    icon: '../../static/images/message/ads.png',
    id: 1,
    typed: 'ads',
    time: new Date(),
    sum: 1,
    message: '精选推荐',
  },
  {
    icon: '../../static/images/message/order.png',
    id: 2,
    typed: 'order',
    time: new Date(),
    sum: 1,
    message: '订单进度',
  },
])

const meesageHandler = (msg: AnyObject) => {
  let info = uni.getStorageSync(msg.typed)
  if (info) {
    msg.sum += JSON.parse(info).sum
  }

  for (let index = 0; index < messageList.value.length; index++) {
    if (messageList.value[index].typed == msg.typed) {
      messageList.value[index].sum += msg.sum
      messageList.value[index].time += msg.time
      console.log(1)
      break
    }
  }
  uni.setStorage({
    key: msg.typed,
    data: JSON.stringify(msg),
  })
}

// 页面周期函数--监听页面加载
onLoad(() => {})

// 页面周期函数--监听页面初次渲染完成
onReady(() => {
  uni.connectSocket({
    url: 'ws://localhost:9250/ws',
    header: {
      'content-type': 'application/json',
    },
    success(res) {
      console.log(res)
      uni.onSocketOpen((result) => {
        console.log('WebSocket连接已打开！')
        uni.sendSocketMessage({
          data: '消息',
        })
      })
      uni.onSocketError(function (res) {
        console.log('WebSocket连接打开失败，请检查！')
      })
    },
  })
  uni.onSocketMessage(function (res) {
    // console.log('收到服务器内容：' + res.data)
    meesageHandler(JSON.parse(res.data))
  })
})

// 页面周期函数--监听页面显示(not-nvue)
onShow(() => {})
// 页面周期函数--监听页面隐藏
onHide(() => {})
// 页面周期函数--监听页面卸载
onUnload(() => {})
</script>

<style scoped lang="scss"></style>
